<?php
/**
 * Created by PhpStorm.
 * User: king
 * Date: 2019/3/2
 * Time: 14:45
 */

namespace app\index\controller;

use think\Db;
use think\Session;
use think\Controller;

class Reals extends Controller
{
//    实名认证
    public function shiming(){

        $search = input('get.');
        $where = [];
        if( isset($search['uid']) && $search['uid']){
            $where['f_user.uid'] = $search['uid'];
        }
        if( isset($search['phone']) && $search['phone']){
            $where['f_user.phone'] = $search['phone'];
        }
        if( isset($search['uname']) && $search['uname']){
            $where['f_user.uname'] = $search['uname'];
        }

        if(isset($search['datemin']) && $search['datemax']!="" && isset($search['datemin']) && $search['datemax']!=""){
            $min = $search['datemin'];
            $max = $search['datemax'];

            $where['f_user.createtime'] = ['between',[$min,$max]];
        }
        //print_r($where);die;
    	  $res=db('user')
                ->join('f_real','f_user.uid=f_real.uid','left')
                ->join('f_package','f_real.tid=f_package.tid','left')
                ->where('f_real.tid','neq',0)
                ->where('phone','neq','')
              ->where($where)
                ->field('f_user.uid,f_real.rid,avatars_img,f_real.tid,phone,head_portrait,starttime,stoptime,Reason,uname,card,front_img,reverse_img,f_real.s_createtime,price,phase_time,rflg')
                ->order('f_real.s_createtime desc');
            $res=$res->paginate(5,false,['query'=>request()->param()]);
            $page=$res->render();
            return view('shiming',['res'=>$res,'page'=>$page,'query'=>$search]);


    }
//实名认证的修改通过
public function suero(){
	$id=input('id');
    $uid=db('real')->where('rid',$id)->value('uid');
    $xname=db('user')->where('uid',$uid)->value('xname');
	$tid=db('real')->where('rid',$id)->value('tid');
	$time=db('package')->where('tid',$tid)->value('phase_time');
	$starttime=date("Y-m-d H:i:s");
	$stoptime=date('Y-m-d H:i:s',strtotime(''.$time.' month'));
	$date=["rflg"=>'1','starttime'=>$starttime,'stoptime'=>$stoptime];
	$rs=db('real')->where('rid',$id)->update($date);
    if($xname!=""){
       $res=db('user')->where('uid',$uid)->update(['uname'=>$xname]);
      if($rs || $res){
			return array('code'=>1,'msg'=>'成功');
       }
        else{
            return array('code'=>0,'msg'=>'失败');		
       }
    }else{
    	if($rs){
			return array('code'=>1,'msg'=>'成功');
        }
        else{
            return array('code'=>0,'msg'=>'失败');		
        }
    }
	
}
//实名认证的修改失败
	public function erro(){
		if(request()->isAjax()){
			$date=["rflg"=>'2','Reason'=>input('txt')];
			$id=input('rid');
			if(db('real')->where('rid',$id)->update($date)){
				return array('code'=>1,'msg'=>'删除成功');
			}
		}
		else{
			$id=input('id');
		    return view('Reason',['rid'=>$id]);   
			
		}
		
	}
//实名认证查看详情
    public function details(){
		$id=input('id');
		$res=db('user')
		->join('f_real','f_user.uid=f_real.uid','left')
		->join('f_package','f_real.tid=f_package.tid','left')
		->where('rid',$id)
		->field('f_user.uid uid,phone,uname,xname,sex,f_real.rid rid,front_img,reverse_img,front_img,starttime,stoptime,phase_time,price')
		->select();
        if($res['0']['uname']==""){
        	$res['0']['uname']=$res['0']['xname'];
        }
        if($res['0']['starttime']==""){
        	$res['0']['starttime']=date("Y-m-d H:i:s");
        }
       if($res['0']['stoptime']==""){
          $time=$res['0']['phase_time'];
        	$res['0']['stoptime']=date('Y-m-d H:i:s',strtotime(''.$time.' month'));
        }
		return view('detalis',['res'=>$res]);
	}
//套餐显示
	public function attest(){
		$res=db('package')->where('flg',1)->order('tid asc')->select();
		$arr=db('privilege')->order('id asc')->select();
		  return view('attest',['res'=>$res,'arr'=>$arr]);
	}
//套餐删除
    public function tdel(){
		$tid=input('id');
		if(db('package')->where('tid',$tid)->update(['flg'=>0]))
			return array('code'=>1,'msg'=>'删除成功');
		else
			return array('code'=>0,'msg'=>'删除失败');
	}
//套餐添加
	public function tadd(){
	   if(request()->isAjax()){
		   $date=["phase_time"=>input('moms'),"price"=>input('mon'),"state"=>input('txt'),'flg'=>1];
		   $db=db('package')->insert($date);
			if($db){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return 0;
			}
	   }
	   else{
		 return view('tadd');   
	   }
	}
//特权添加
    public function cadd(){
	   if(request()->isAjax()){
		   $date=["privilege"=>input('pos'),"state"=>input('state')];
			if(db('privilege')->insert($date)){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return 0;
			}
	   }
	   else{
		 return view('cadd');   
	   }
	}
//特权删除
    public function cdel(){
		$id=input('id');
		if(db('privilege')->where('id',$id)->delete())
			return array('code'=>1,'msg'=>'删除成功');
		else
			return array('code'=>0,'msg'=>'删除失败');
	}
//帮你踩
	public function bangcai(){
        $search = input('get.');

        $where = [];
        if( isset($search['cid']) && $search['cid']){
            $where['f_bangcai.id'] = $search['cid'];
        }

        if( isset($search['phone']) && $search['phone']){
            $where['f_user.phone'] = $search['phone'];
        }
        if( isset($search['uname']) && $search['uname']){
            $where['f_user.nickname'] = $search['uname'];
        }

        if(isset($search['datemin']) && $search['datemax']!="" && isset($search['datemin']) && $search['datemax']!=""){
            $min = $search['datemin'];
            $max = $search['datemax'];

            $where['f_bangcai.createtime'] = ['between',[$min,$max]];
        }

		$res=db('bangcai')
                ->join('f_user','f_bangcai.uid=f_user.uid')
                ->field('f_bangcai.*,f_user.uname,f_user.nickname,head_portrait,f_user.phone,f_user.avatars_img')
            ->where($where)
                ->order('f_bangcai.id desc');
		 $rs=db('bangcai')
                ->join('f_user','f_bangcai.uid=f_user.uid')
             ->where($where)
                ->count();
        $res=$res->paginate(10,false,['query'=>request()->param()]);
        $page=$res->render();
        return view('bangcai',['res'=>$res,'page'=>$page,'rs'=>$rs,'query'=>$search]);
    }
//帮你踩改变未处理与为处理状态
    public function updateflg(){
		$id=input('id');
		$date=["flg"=>'1'];
		$rs=db('bangcai')->where('id',$id)->update($date);
		if($rs){
			return 1;
		}
        else{
			return 0;		
		}
	}
//帮你踩设置
	public function bncset(){
		if(request()->isAjax()){
			$date=["workdate"=>input('workdate'),"way"=>input('way'),"tel"=>input('tel')];
            $rs=db('bangcais')->where('id',1)->update($date);
			if($rs){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return array('code'=>0,'msg'=>'失败');
			}
		}else{
           $res=db('bangcais')->find();
			return view('bncset',['res'=>$res]);
		}
        
    }
//委托采购
	public function caigou(){
        $search = input('get.');
        $where = [];
        if( isset($search['cid']) && $search['cid']){
            $where['f_caigou.id'] = $search['cid'];
        }
        if( isset($search['uid']) && $search['uid']){
            $where['f_user.uid'] = $search['uid'];
        }
        if( isset($search['phone']) && $search['phone']){
            $where['f_user.phone'] = $search['phone'];
        }
        if( isset($search['uname']) && $search['uname']){
            $where['f_user.nickname'] = $search['uname'];
        }

        if(isset($search['datemin']) && $search['datemax']!="" && isset($search['datemin']) && $search['datemax']!=""){
            $min = $search['datemin'];
            $max = $search['datemax'];

            $where['f_caigou.createtime'] = ['between',[$min,$max]];
        }

		$res=db('caigou')
                ->join('f_user','f_caigou.uid=f_user.uid')
                ->field('f_caigou.*,f_user.uname,f_user.phone,f_user.nickname,head_portrait,f_user.avatars_img')
            ->where($where)
                 ->order('f_caigou.id desc');
		$rs=db('caigou')
            ->where($where)
                ->join('f_user','f_caigou.uid=f_user.uid')
                ->count();
        $res=$res->paginate(10,false,['query'=>request()->param()]);
        $page=$res->render();
        return view('caigou',['res'=>$res,'page'=>$page,'rs'=>$rs,'query'=>$search]);
        
    }
	//委托采购改变未处理与为处理状态
    public function upcflg(){
		$id=input('id');
		$date=["flg"=>'1'];
		$rs=db('caigou')->where('id',$id)->update($date);
		if($rs){
			return 1;
		}
        else{
			return 0;		
		}
	}
	//委托采购设置
	public function wtcgset(){
		if(request()->isAjax()){
			$date=["workdate"=>input('workdate'),"way"=>input('way'),"tel"=>input('tel')];
            $rs=db('caigous')->where('id',1)->update($date);
			if($rs){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return array('code'=>0,'msg'=>'失败');
			}
		}else{
             $res=db('caigous')->find();
			return view('wtcgset',['res'=>$res]);
		}
        
    }
	
}